# 安装 k8s

## 禁止swap分区

```

sed -i "s/\/swap.img/# \/swap.img/g" /etc/fstab

cat /etc/fstab

sudo swapoff -a

```

## 配置

```



(
cat << 'EOF'
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
) >/etc/sysctl.d/k8s.conf

sudo sysctl --system



```

## 安装

```



sudo apt-get update && sudo apt-get install -y ca-certificates curl software-properties-common apt-transport-https curl

curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - 

(
cat << 'EOF'
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
) >/etc/apt/sources.list.d/kubernetes.list


sudo apt-get update

# 查看可安装版本
# apt-cache madison kubelet
# apt-cache madison kubeadm


#sudo apt-get remove kubelet kubeadm kubectl

apt-get install -y kubelet=1.18.0-00 kubeadm=1.18.0-00 kubectl=1.18.0-00

sudo apt-mark hold kubelet kubeadm kubectl

sudo systemctl enable kubelet && sudo systemctl start kubelet


```

## 安装集群:master


```
# kubeadm config images list --kubernetes-version=v1.18.0



kubeadm init --v=5 --apiserver-advertise-address=15.142.2.30 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.18.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16


# kubeadm init --config kubeadm-config.yaml --upload-certs --v=5

# kubeadm init --kubernetes-version=v1.18.0 --apiserver-advertise-address 15.142.2.31 --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=all



Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

 mkdir -p $HOME/.kube
 sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
 sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
 https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 15.142.2.30:6443 --token 9v5ip8.3lb2irs53gs9r2la \
  --discovery-token-ca-cert-hash sha256:0b645aae5270acc68ff43e68f8a5d7079cd40480c59a2bceb9d2033b44484b85




kubectl get nodes



```

## 部署CNI网络插件

```


mkdir -p /cardone/middlewares/k8s && chown -R cardone:cardone /cardone/middlewares/k8s

kubectl apply -f /cardone/middlewares/k8s/kube-flannel.yml

kubectl get pods -n kube-system



```

## test

```



kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc


kubectl delete services nginx



```

## 安装可视化管理




```


mkdir -p /cardone/middlewares/kubernetes-dashboard && chown -R cardone:cardone /cardone/middlewares/kubernetes-dashboard

# kubectl delete -f /cardone/middlewares/kubernetes-dashboard/recommended.yaml

kubectl create -f /cardone/middlewares/kubernetes-dashboard/recommended.yaml


kubectl get pod,svc -n kubernetes-dashboard



kubectl create -f /cardone/middlewares/kubernetes-dashboard/adminuser.yaml



kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

https://15.142.2.30:30443

eyJhbGciOiJSUzI1NiIsImtpZCI6ImwyZVdtTEVDYi16MHZNUTlZMTF2WF92THNocHBiR1lDVkN6NWVYOUJPLTgifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLTliOW5iIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI5MDM4YjI3YS1iNDQ3LTQ5ZjMtYjYyMC03OTYxZmE4N2ViZGQiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.Ev10N2HbHhtR--XHZllLfcuOLFcMJLTHLFd_7wP-Pd9PcJqNnmHnvx1lek-7nIZ7bJDOeHNc8IBCYfGFrRDQxpZIWcEQEbt6J-RRbIj6_Yp5wfkh7QW6ma3Vhs1KtXKMsvr5RWC12wKDV-ZBefBY84M1yRQbxdHA4Zlzx8V2JKQlaye3oRk3q6SIk5LYEabKq6d6KLVnxza5rNvv_WDQryOCaAXSfDCZAm4hXRCSJ50hAndfS5x6kTgfdUH5foW2GI0U0O2WdMf63RNbxxip0OcQEdiQLQh7roiX_OVYqqQjfz2-mY-1vOdiDhFWC_9jsV0gLvbHBVvGF5OOCy-Xmg


kubectl create namespace cardone


kubectl get namespace


```

### 安装 helm

```


mkdir -p /cardone/temps && chown -R cardone:cardone /cardone/temps

cd /cardone/temps

wget https://get.helm.sh/helm-v3.6.1-linux-amd64.tar.gz

tar -zxvf helm-v3.6.1-linux-amd64.tar.gz

mv /cardone/temps/linux-amd64/helm /usr/local/bin/helm

helm help

#helm repo add stable https://kubernetes.oss-cn-shenzhen.aliyuncs.com/charts

helm repo add bitnami https://charts.bitnami.com/bitnami

helm repo update

```
